<template>
  <el-dialog :visible.sync="visible"
             :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-row :gutter="20">
      <el-col :span="6">
        <el-select v-model="selectedRole"
                   default-first-option
                   filterable
                   clearable
                   :placeholder="$t('custom.tenant.assignRole.selectRole')">
          <el-option v-for="e in comDefaultRoleList"
                     :key="e.id"
                     :label="e.name"
                     :value="e.id"></el-option>
        </el-select>
      </el-col>
      <el-col :span="6">
        <el-button size="small"
                   type="primary"
                   :loading='addBtnLoading'
                   @click="addRoleItem">{{ $t('custom.tenant.assignRole.addBtn') }}</el-button>
      </el-col>
    </el-row>
    <el-row :gutter="20"
            style="min-height:200px;">
      <el-col :span="24">
        <h3>{{ $t('custom.tenant.assignRole.existingRoleList') }}</h3>
        <el-tag v-for="item in clientRolesList"
                :key="item.roleId"
                :closable="isCanClosed(item)"
                @close="clearRoleItem(item.id)"
                type="info"
                style="margin-right:14px;margin-bottom:14px;">
          {{ item.name }}
        </el-tag>
      </el-col>
    </el-row>
    <template slot="footer">
      <el-button @click="visible = false"
                 size="small">{{ $t('cancel') }}</el-button>
      <!-- <el-button type="primary"
                 size="small"
                 @click="saveHandle()">{{ $t('confirm') }}</el-button> -->
    </template>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      clientId: null,
      selectedRole: null,
      defaultRolesList: [],
      clientRolesList: [],
      addBtnLoading: false
    }
  },
  computed: {
    comDefaultRoleList () {
      let temp = []
      for (let i = 0; i < this.defaultRolesList.length; i++) {
        let hasIndex = this.clientRolesList.findIndex(e => e.roleId === this.defaultRolesList[i].id)
        if (hasIndex < 0) {
          temp.push(this.defaultRolesList[i])
        }
      }
      return temp
    }
  },
  methods: {
    init () {
      this.getDefaultRolesList()
      this.getClientRolesList()
    },
    openDialog (id) {
      this.clientId = id
      this.init()
      this.visible = true
    },
    // saveHandle () {
    // },
    clearRoleItem (roleId) {
      this.$interface.system.deleteClientRoleListApi(roleId)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message.success(res.msg)
          this.getClientRolesList()
        })
        .catch(e => { this.$message.error(e.msg) })
    },
    addRoleItem () {
      this.addBtnLoading = true
      const params = {
        roleId: this.selectedRole,
        tenantCode: this.clientId
      }
      this.$interface.system.saveClientRoleListApi(params)
        .then(({ data: res }) => {
          this.addBtnLoading = false
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message.success(res.msg)
          this.selectedRole = null
          this.getClientRolesList()
        })
        .catch(e => {
          this.$message.error(e.msg)
          this.addBtnLoading = false
        })
    },
    isCanClosed (item) {
      if (item.id !== '0' && item.roleId !== '1125415693534105602' && item.roleId !== '1266700345287905282') {
        return true
      } else {
        return false
      }
    },
    getDefaultRolesList () {
      this.$interface.system.getDefaultRoleListApi()
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.defaultRolesList = res.data
        })
        .catch(e => { this.$message.error(e.msg) })
    },
    getClientRolesList () {
      const params = {
        tenantCode: this.clientId
      }
      this.$interface.system.getClientRoleListApi(params)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.clientRolesList = res.data
        })
        .catch(e => { this.$message.error(e.msg) })
    }
  }
}
</script>

<style lang="scss">
</style>
